package com.fy56.platform.logistics.service.impl;

import com.fy56.platform.logistics.common.ResultModel;
import com.fy56.platform.logistics.common.ResultStatus;
import com.fy56.platform.logistics.mapper.MiniProgramMapper;
import com.fy56.platform.logistics.model.MiniProgram;
import com.fy56.platform.logistics.service.web.IMiniProgramService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 小程序项目Service实现
 * @author li
 * @date 2018-02-08 16:58
 */
@Service
public class MiniProgramServiceImpl implements IMiniProgramService {

    @Autowired
    private MiniProgramMapper miniProgramMapper;

    @Override
    public MiniProgram selectByCode(String tenantCode) {
        return miniProgramMapper.selectByCode(tenantCode);
    }

    @Override
    public MiniProgram selectByAppId(String appId) {
        return miniProgramMapper.selectByAppId(appId);
    }

    @Override
    public MiniProgram selectByPrimaryKey(Long id) {
        return miniProgramMapper.selectByPrimaryKey(id);
    }

    @Override
    public MiniProgram get(MiniProgram entity) {
        return null;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel insert(MiniProgram entity) {
        ResultModel result = null;
        int r = miniProgramMapper.insert(entity);
        if(r == 0) {
            result = ResultModel.error(ResultStatus.INSERT_ERR);
        }else {
            result = ResultModel.ok();
        }
        return result;
    }

    @Override
    @Transactional(readOnly = true)
    public ResultModel insertSelective(MiniProgram entity) {
        return null;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel update(MiniProgram entity) {
        ResultModel result = null;
        int r = miniProgramMapper.updateByPrimaryKey(entity);
        if (r == 0) {
            result = new ResultModel(ResultStatus.EDIT_ERR);
        }else {
            result = ResultModel.ok();
        }
        return result;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel updateSelective(MiniProgram entity) {
        ResultModel result = null;
        int r = miniProgramMapper.updateByPrimaryKeySelective(entity);
        if (r == 0) {
            result = new ResultModel(ResultStatus.EDIT_ERR);
        }else {
            result = ResultModel.ok();
        }
        return result;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel delete(Long id) {
        int r = miniProgramMapper.deleteByPrimaryKey(id);
        if (r == 0){
            return new ResultModel(ResultStatus.DELETE_ERR);
        }
        return ResultModel.ok();
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel delete(MiniProgram entity) {
        int r = miniProgramMapper.updateByPrimaryKeySelective(entity);
        if (r == 0){
            return new ResultModel(ResultStatus.DELETE_ERR);
        }
        return ResultModel.ok();
    }

    @Override
    public List<MiniProgram> findList(MiniProgram entity) {
        List<MiniProgram> list = miniProgramMapper.findList(entity);
        return list;
    }

    @Override
    @Transactional(readOnly = false)
    public ResultModel batchDelete(ArrayList<MiniProgram> list) {
        LinkedList<String[]> failedIds = new LinkedList<String[]>();
        int r = 0;
        for (MiniProgram model : list) {
            r = miniProgramMapper.updateByPrimaryKeySelective(model);
            if (r == 0) {
                failedIds.add(new String[]{model.getId().toString(), "批量删除失败!"});
            }
        }
        if(failedIds.size() > 0){
            return new ResultModel(0, "", failedIds);
        }else{
            return ResultModel.ok();
        }
    }
}
